<?php

namespace Visitors;

use Pinenut\Action;
use Users\Profile;
use Visitors\VisitsInfo;
use \Log;

class IndexAction extends \Action {
    protected static $isPublic = true;
    
	protected static $layout = "#layouts.common";

	/**
	 * @var unknown
	 * количество визиторов на страницу
	 */
	static $countOnPage = 10;
	function privileges() {
		return '#authorized';
	}
	/**
	 * Указываем необходимые неявные параметры
	 * @return multitype:string
	 */
	static function pullParams() {
		return ['identity'];
	}

	protected function run() {
		$currentPage = $this->request->page;
		// for first calling default page
		if (! $this->request->page) {
			$currentPage = 1;
		}
		
		$identity = $this->request->_identity;
		$this->response->currentProfileId = $identity;
		
		$finder = VisitsInfo::find ();
		if ($finder->where ( 'profile._ref', $identity )->count () > 0) {
			$objVisitInfo = $finder->where ( 'profile._ref', $identity )->one ();
			$objVisitInfo->lastViewOwner = time ();
			$objVisitInfo->unSeenCount = 0;
			$objVisitInfo->save ();
		}
		
		// $visitors = VisitsInfo::pageVisitors($activPage,$this->request->id);
		$this->response->page = VisitsInfo::pageVisitors ( $currentPage, $identity, self::$countOnPage );
		
		$guestsCount = VisitsInfo::CountOfVisitors ( ( string ) $identity );
		
		$pages = ceil ( $guestsCount / self::$countOnPage );
		
		$this->response->pages = $pages;
		
		$this->response->currentPage = $currentPage;
	}
}
